package org.sqldroid;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;

import android.database.Cursor;

public class SqldroidResultSetMetaData implements ResultSetMetaData {
	private final Cursor cursor;

	public SqldroidResultSetMetaData(final Cursor cursor) {
		if (cursor == null) {
			throw new RuntimeException("Cursor required to be not null.");
		}
		this.cursor = cursor;
	}

	public String getCatalogName(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return null;
	}

	public String getColumnClassName(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return null;
	}

	public int getColumnCount() throws SQLException {
		final int columnCount = cursor.getColumnCount();
		return columnCount;
	}

	public int getColumnDisplaySize(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return 0;
	}

	public String getColumnLabel(final int column) throws SQLException {
		return cursor.getColumnName(column - 1);
	}

	public String getColumnName(final int column) throws SQLException {
		return cursor.getColumnName(column - 1);
	}

	public int getColumnType(final int column) throws SQLException {
		cursor.getPosition();
		if (cursor.isBeforeFirst() || cursor.isAfterLast()) {
			final boolean resultSetEmpty = cursor.getCount() == 0 || cursor.isAfterLast();
			if (resultSetEmpty) {
				return Types.NULL;
			}
			cursor.moveToFirst();
		}
		// int nativeType = cursor.getType(column - 1);
		// int type;
		// switch (nativeType) {
		// case Cursor.FIELD_TYPE_NULL:
		// type = Types.NULL;
		// break;
		// case Cursor.FIELD_TYPE_INTEGER:
		// type = Types.INTEGER;
		// break;
		// case Cursor.FIELD_TYPE_FLOAT:
		// type = Types.FLOAT;
		// break;
		// case Cursor.FIELD_TYPE_STRING:
		// type = Types.VARCHAR;
		// break;
		// case Cursor.FIELD_TYPE_BLOB:
		// type = Types.BLOB;
		// break;
		// default:
		// type = Types.NULL;
		// break;
		// }
		// if (moved) {
		// cursor.moveToPosition(oldPos);
		// }
		return 0;
	}

	public String getColumnTypeName(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return null;
	}

	public int getPrecision(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return 0;
	}

	public int getScale(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return 0;
	}

	public String getSchemaName(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return null;
	}

	public String getTableName(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return null;
	}

	public boolean isAutoIncrement(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isCaseSensitive(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isCurrency(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isDefinitelyWritable(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public int isNullable(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return 0;
	}

	public boolean isReadOnly(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isSearchable(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isSigned(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isWritable(final int column) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public boolean isWrapperFor(final Class<?> arg0) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return false;
	}

	public <T> T unwrap(final Class<T> arg0) throws SQLException {
		System.err.println(" ********************* not implemented @ " + DebugPrinter.getFileName() + " line "
				+ DebugPrinter.getLineNumber());
		return null;
	}

}
